package com.tiantian.novel.service;

import com.tiantian.novel.helper.NovelHelper;
import com.tiantian.novel.model.NovelSummary;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.io.File;
import java.io.IOException;

/**
 * @author wangkai
 * @since 2020/12/17.
 */
@Component
public class NovelService implements InitializingBean {
    private static final Logger log = LoggerFactory.getLogger(NovelService.class);

    @Value("${kindle.home:/tmp/kindle_home}")
    private String kindleHome;
    @Value("${novel.home:/tmp/novel_home}")
    private String novelHome;
    private NovelHelper novelHelper;

    @Override
    public void afterPropertiesSet() {
        this.novelHelper = new NovelHelper(kindleHome, novelHome);
    }

    /**
     * Get novel info
     *
     * @param url
     *
     * @return
     * @throws IOException
     */
    public NovelSummary query(String url) throws Exception {
        return novelHelper.loadCover(url);
    }

    /**
     * 爬取html
     *
     * @param url
     *
     * @return
     * @throws IOException
     */
    public NovelSummary fetch(String url) throws Exception {
        NovelSummary novelSummary = novelHelper.loadCover(url);
        _asyncFetch(novelSummary);
        log.info("fetch 【{}】", url);
        return novelSummary;
    }

    private void _asyncFetch(NovelSummary novelSummary) {
        new Thread() {
            @Override
            public void run() {
                try {
                    novelHelper.fetch(novelSummary);
                } catch (Exception e) {
                    log.error("Async fetch() failed", e);
                }
            }
        }.start();
    }

    /**
     * 爬取 & 生成mobi
     */
    public String fetchAndGenMobi(String url) throws Exception {
        NovelSummary novel = fetch(url);
        _genMobi(novel, novelHome);
        return novel.getCode();
    }

    /**
     * 生成mobi
     *
     * @param url
     *
     * @return
     */
    public String genMobi(String url) throws Exception {
        NovelSummary novel = novelHelper.loadChapters(url);
        _genMobi(novel, novelHome);
        return novel.getCode();
    }

    private void _genMobi(NovelSummary novel, String store) {
        new Thread() {
            @Override
            public void run() {
                try {
                    novelHelper.genMobi(novel);
                } catch (Exception e) {
                    log.error("Async genMobi() failed ", e);
                }
            }
        }.start();
    }

    /**
     * 下载
     *
     * @param url
     *
     * @return
     */
    public File downloadMobi(String url) throws Exception {
        NovelSummary novel = query(url);
        File file = novelHelper.getMobi(novel);
        return file;
    }

    public NovelSummary fetchByCode(String code) {
        log.info("fetch by code:{}", code);
        NovelSummary novelSummary = novelHelper.loadCoverFromLocal(code);
        _asyncFetch(novelSummary);
        log.info("fetch finished", code);
        return novelSummary;
    }
}
